package com.infinit.easyreader.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.infinit.easyreader.R;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 引导界面Activity
 */
public class MainActivity extends AppCompatActivity {


    private static final int DISPLAY_LOADING_TIME = 1;
    private static final int START_ACTIVITY = 2;
    @Bind(R.id.tv_loading_time)
    TextView tvLoadingTime;
    @Bind(R.id.rlBack)
    RelativeLayout rlBack;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            //缩放指定的图片，减少内存占用


            mHandler = new Handler(new Handler.Callback() {
                @SuppressLint("SetTextI18n")
                @Override
                public boolean handleMessage(Message msg) {
                    switch (msg.what) {
                        case DISPLAY_LOADING_TIME:
                            tvLoadingTime.setText(msg.arg1 + "s");
                            break;
                        case START_ACTIVITY:
                            Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                            MainActivity.this.startActivity(intent);
                            MainActivity.this.finish();
                            break;
                    }
                    return true;
                }
            });
            new LoadingThread().start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 类：LoadingThread
     * 作用：发送消息更新首页的Loading数字
     */

    class LoadingThread extends Thread {
        private int count = 3;

        @Override
        public void run() {
            while (count > 0) {
                Message.obtain(mHandler, DISPLAY_LOADING_TIME, count, 0).sendToTarget();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count--;
            }
            Message.obtain(mHandler, START_ACTIVITY).sendToTarget();
        }
    }


}
